package com.nyist.myticket.biz.service.impl;

import com.nyist.myticket.biz.service.ToMailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangxiaogen
 * @date 2020/12/22 17:01
 */
@Service
public class ToMailServiceImpl implements ToMailService {

    private static Logger logger = LoggerFactory.getLogger(ToMailServiceImpl.class);

    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;

    @Override
    public void sendMail(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        try {
            javaMailSender.send(message);
            logger.info("-----------------发送成功!!!\n-----------------对方是:"+to);
        } catch (Exception e){
            logger.error("发送邮件异常",e);
        }
    }

    @Override
    public void quartzToMail(String subject, String text) {
        logger.info("-------------------- 定时发送任务 --------------------");
        String to = "1908251694@qq.com";
        String to1 = "940937065@qq.com";
        //String to2 = "2863808949@qq.com";
        //String to3 = "2374023890@qq.com";
        String to4 = "2403787438@qq.com";
        List<String> stringList = new ArrayList<String>();
        stringList.add(to);
        stringList.add(to1);
        //stringList.add(to2);
        //stringList.add(to3);
        stringList.add(to4);

        stringList.parallelStream().forEach(s -> {
            try {
                SimpleMailMessage message = new SimpleMailMessage();
                message.setFrom(from);
                //message.setTo(to);
                //message.setTo(to,to1,to4);
                message.setSubject(subject);
                message.setText(text);
                message.setTo(s);
                javaMailSender.send(message);
                logger.info("-----------------发送成功!!!\n-----------------对方是:"+s);
            } catch (Exception e){
                logger.error("发送邮件异常",e);
            }
        });
    }
}
